//
// main.cpp
// ~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

#include <iostream>
#include <string>
#include <vector>
#include <boost/asio.hpp>


#include "Server.h"
#include "MySqlConnector.h"
#include "ConfigReader.h"
#include "ServerUtil.h"
#include "UserInfo.h"
#include "LogManager.h"

int testConfigReader()
{
	ConfigReader reader;
	reader.load("app.properties");
	return 0;
}

//  argc : number of argument; argv array of parameter
int testServer(int argc, char** argv)
{
	try
	{
        // config log4cpp


		// Check command line arguments.
		/*if (argc != 4)
		{
		std::cerr << "Usage: http_server <address> <port> <doc_root>\n";
		std::cerr << "  For IPv4, try:\n";
		std::cerr << "    StartServer 0.0.0.0 80 .\n";
		std::cerr << "  For IPv6, try:\n";
		std::cerr << "    StartServer 0::0 80 .\n";
		return 1;
		}
		*/
		// Initialise the server.
        LOG->getLogger().info("Start Server");
		ConfigReader reader;
		auto success = reader.load("app.properties");

		if (!success)
		{
			std::cerr << " Load config file fail with code : ";
#ifdef _WIN32
			 std::cerr << GetLastError();
#endif
			return -1;
		}

		auto && host = reader.getProperty("server_host");
		auto && port = reader.getProperty("server_port");
		auto  rootDir = reader.getProperty("root_dir");

		http::server::Server s(host, port, rootDir);

		// Run the server until stopped.
		s.run();
	}
	catch (std::exception& e)
	{
		std::cerr << "exception: " << e.what() << "\n";
	}

	return 0;
}

//
//int testSql()
//{
//	std::auto_ptr<MySql::MySqlConnector> connector = std::auto_ptr<MySql::MySqlConnector>(new MySql::MySqlConnector());
//	auto connected = connector->connect("127.0.0.1", "root", "@dminlinux");
//	std::cout << "connected :" << connected;
//	connector->setSchema("EnglishForChild");
//
//	ServerUtil::getInstance()->isValidUser(*connector, UserInfo("admin", "admin"));
//
//	return 0;
//}

int main(int argc, char* argv[])
{
	//testConfigReader();
	//testSql();
	//testServer(argc, argv);
	//testXml();
	testServer(argc, argv);
	return 0;
}
